package com.yanwq.sayhi.rest;

import com.yanwq.sayhi.bean.Article;
import com.yanwq.sayhi.bean.User;
import com.yanwq.sayhi.service.ArticleService;
import com.yanwq.sayhi.tool.JSONResultTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.UUID;

/**
 * 作者：yanweiqiang。
 * 日期：2016/2/2。
 */
@RestController
@RequestMapping(path = "/article")
public class ArticleRest extends BaseRest {
    @Autowired
    private ArticleService service;

    @RequestMapping(path = "/count")
    public String countByPublishTime(long refreshTime) {
        return JSONResultTool.makeJSONResult(service.countByPublishTime(refreshTime));
    }

    @RequestMapping(path = "/count/club")
    public String countByPublishTimeAndClub(long refreshTime, long clubId) {
        return JSONResultTool.makeJSONResult(service.countByPublishTimeAndClubId(refreshTime, clubId));
    }

    @RequestMapping(path = "/count/clubs")
    public String countByPublishTime(long refreshTime, List<Long> clubIds) {
        return service.listClubNewArticleCount(refreshTime, clubIds).toString();
    }

    @RequestMapping(path = "/add")
    public Article add(@RequestBody Article article) {
        article.setUuid(UUID.randomUUID().toString());
        article.setPermission(0);
        article.setPublishTime(System.currentTimeMillis());
        return service.save(article);
    }

    @RequestMapping(path = "/add/club")
    public Article addByClub(@RequestBody Article article) {
        article.setUuid(UUID.randomUUID().toString());
        article.setPermission(1);
        article.setPublishTime(System.currentTimeMillis());
        return service.save(article);
    }

    @RequestMapping(path = "/add/user")
    public Article addByUser(@RequestBody Article article) {
        article.setUuid(UUID.randomUUID().toString());
        article.setPermission(2);
        article.setPublishTime(System.currentTimeMillis());
        return service.save(article);
    }

    @RequestMapping(path = "/list")
    public List<Article> list(int page, int size, int type, String token) {
        validateToken(token);

        if (type == -1) {
            return service.listByPermission(page, size, 0, token);
        } else {
            return service.listByPermissionAndType(page, size, 0, type, token);
        }
    }

    @RequestMapping(path = "/list/club")
    public List<Article> listByClub(int page, int size, long clubId, int type, String token) {
        if (type == -1) {
            return service.listByClub(page, size, clubId, token);
        } else {
            return service.listByClubAndType(page, size, clubId, type, token);
        }
    }

    @RequestMapping(path = "/list/user")
    public List<Article> listByUser(int page, int size, User user, String token) {
        return service.listByUser(page, size, user, token);
    }
}
